package cn.enjoyedu.fksum.dao;


import cn.enjoyedu.fksum.service.MakeArray;
import cn.enjoyedu.fksum.service.SingleThread;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Random;

/**
 * 类说明：模拟数据库操作
 */
@Service
public class DbUtils {

    @Autowired
    private MakeArray makeArray;

    public int getMin(){
        String sql = "select min(id) from goods_info";
        Random r = new Random();
        return r.nextInt(10)+10;
    }

    public int getMax(){
        String sql = "select max(id) from goods_info";
        Random r = new Random();
        return r.nextInt(10000000);
    }

    /**
     * @param sql  要执行的sql
     * @param length  限定的记录条数
     * @return
     */
    public int[] getRecords(String sql,int length){
        try {
            Thread.sleep(15);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return makeArray.makeArray(length);
    }

    /**
     * 在数据库中获得指定数目的记录，然后进行累加
     * @param fromId   记录开始id
     * @param toId  记录结束id
     * @return  累积的和
     */
    public long sumRecorders(int fromId,int toId){
        long sum = 0;
        String sql = "select goods_count from goods_info where id >= "+fromId
                +" and id < "+toId;
        try {
            Thread.sleep(15);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        int[] result = makeArray.makeArray(SingleThread.THRESHOLD);
        for(int record:result){
            sum = sum + record;
        }
        return sum;
    }

}
